<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <!-- import CSS -->
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
    <script src="./js/jquery-3.5.1/jquery-3.5.1.js"></script>
</head>
<body>
<div id="app">
    <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm" style="width: 500px">
        <el-form-item label="账号" prop="userAccount">
            <el-input v-model="ruleForm.userAccount" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="userPwd">
            <el-input type="password" v-model="ruleForm.userPwd" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')">注册</el-button>
            <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
    </el-form>
</div>
</body>
<!-- import Vue before Element -->
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<!-- import JavaScript -->
<script src="https://unpkg.com/element-ui/lib/index.js"></script>
<script>
    var Main = {
        data() {
            var validateAccount = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入账号'));
                } else {
                    callback();
                }
            };
            var validatePass = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入密码'));
                } else {
                    callback();
                }
            };
            return {
                ruleForm: {
                    userAccount: '',
                    userPwd: '',
                },
                rules: {
                    userAccount: [
                        { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' },
                        { validator: validateAccount, trigger: 'blur' }
                    ],
                    userPwd: [
                        { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' },
                        { validator: validatePass, trigger: 'blur' }
                    ],
                }
            };
        },
        methods: {
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        alert('submit!');
                        console.log(Main.data().ruleForm.userAccount);
                        this.doRegistry();
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            },
            doRegistry(){
                let account = this.ruleForm.userAccount;
                let pwd = this.ruleForm.userPwd;
                console.log(account);
                console.log(pwd);
                $.ajax({
                    url:'/user/registry',
                    type:'post',
                    data:{
                        userAccount:account,
                        userPwd:pwd
                    },
                    success(res){
                        alert(res);
                    }
                })
            }
        }
    };
    var Ctor = Vue.extend(Main)
    new Ctor().$mount('#app')
</script>
</html>